package com.simplehc7878.warning.strategy.impl;

import com.simplehc7878.warning.converter.DataConverter;
import com.simplehc7878.warning.domain.dto.DataMetricsDTO;
import com.simplehc7878.warning.domain.dto.MetaDataDTO;
import com.simplehc7878.warning.domain.dto.WarningResultDTO;
import com.simplehc7878.warning.domain.model.WarningRule;
import com.simplehc7878.warning.enums.RuleType;
import com.simplehc7878.warning.service.DataService;
import com.simplehc7878.warning.strategy.WarningStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据指标报警策略
 */
@Component
public class DataMetricsWarningStrategy implements WarningStrategy {

    @Autowired
    private DataService dataService;

    @Override
    public List<WarningResultDTO> run(WarningRule rule) {
        List<WarningResultDTO> resultDTOS = new ArrayList<>();
        RuleType ruleType = RuleType.getByValue(rule.getRuleType());
        List<MetaDataDTO> metaDataDTOList = dataService.getConsumption(ruleType);
        List<DataMetricsDTO> dataMetricsDTO = DataConverter.toDataMetricsDTO(metaDataDTOList);
        if (!CollectionUtils.isEmpty(dataMetricsDTO)) {
            for (DataMetricsDTO metricsDTO : dataMetricsDTO) {
                if (checkUpDown(metricsDTO)) {
                    resultDTOS.add(null);
                }
                if (checkRangeDeviation()) {
                    resultDTOS.add(null);
                }
            }
        }
        return resultDTOS;
    }

    private Boolean checkUpDown(DataMetricsDTO data) {
        if (data.getValue() == null) {
            return true;
        }
        boolean beyondUp = false;
        boolean beyondDown = false;
        if (data.getUp() != null && data.getValue().compareTo(data.getUp()) > 0) {
            beyondUp = true;
        }
        if (data.getDown() != null && data.getValue().compareTo(data.getDown()) < 0) {
            beyondDown = true;
        }
        return beyondUp || beyondDown;
    }

    private Boolean checkRangeDeviation() {
        return false;
    }


}
